package com.masterlu.leetcode.daily.heap.simple;

import java.util.PriorityQueue;

/**
 * 703. 数据流中的第 K 大元素
 * https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/
 *
 * @date created in 2021/5/21 10:50
 */
public class S703KthLargest {

    PriorityQueue<Integer> pq = new PriorityQueue();
    int k= 0;

    public S703KthLargest(int k, int[] nums){

        this.k = k;
        for (int num : nums) {
            pq.offer(num);
        }

    }

    public int add(int val) {

        pq.offer(val);
        while (pq.size() > k){
            pq.poll();
        }

        return pq.peek();
    }
}
